package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    //新增会员
    public void add(Member member) {
         memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport(String starttime,String endtime) {


        //创建存储每月会员人数的集合
        List<Long> memberCount = new ArrayList<>();
        //获取查询的月份
        List<String> months =null;
        if (starttime!=null&&endtime!=null){
            System.out.println("开始时间"+starttime+"结束时间"+endtime);
            try {
                //获取两个日期之间的月份
                months = DateUtils.getMonthBetween(starttime,endtime );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else {
            //获取最近一年的月份
            months=DateUtils.getThisYearMonths();
        }

        String thisMonthLastDay=null;
        //获取每个月对应的会员总数
        for (String month : months) {
            if (starttime==null&&endtime==null){
                month += "-01";
                thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getThisMonthLastDay(month));
            }
                long count = memberDao.getMemberCountByMonth(thisMonthLastDay,starttime,endtime);
                memberCount.add(count);
            }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("months" , months);
        resultMap.put("memberCount" , memberCount);
        return resultMap;
    }

    @Override
    public List<Map> getMemberReportBySex() {
        /*//存放性别
        List<String> sexList=new ArrayList<>();
        sexList.add("男");
        sexList.add("女");
        //存放会员数量
        List<Long> sexCountList = new ArrayList<>();
        Map<String,Object> sexCount=new HashMap<>();//{sex，count}
        //遍历男女两种情况
        for (int i = 1; i <= 2; i++) {
            //根据性别查找对应的会员数量
            long count=memberDao.getMemberCountBySex(i);
            //将性别对应的会员数量存入list集合
            sexCount.put(sexList.get(i-1), count);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("sex", sexList);
        resultMap.put("sexCount" , sexCount);
        return resultMap;*/

        return memberDao.getMemberCountBySex();
    }

    @Override
    public List<Map> getMemberReportByAge() {
        return memberDao.getMemberReportByAge();
        /*int age_0_20=0;
        int age_20_30=0;
        int age_30_40=0;
        int age_40_50=0;
        int age_50_60=0;
        int age_60_=0;
        //<年龄段，数量>
        List<Map> agePhase=new ArrayList<>();
        //获取所有会员
        List<Member> memberList=memberDao.findAll();
        for (Member member : memberList) {
            Date birthday = member.getBirthday();
            int age = getAgeByBirth(birthday);
            if (age<20){
                age_0_20++;
            }
            if (age>20&&age<30){
                age_20_30++;
            }
            if (age>30&&age<40){
                age_30_40++;
            }
            if (age>40&&age<50){
                age_40_50++;
            }
            if (age>50&&age<60){
                age_50_60++;
            }
            if (age>60){
                age_60_++;
            }

        }

        Map<String,Object> ageCount=new HashMap<>();

        ageCount.put("0~20",age_0_20);
        ageCount.put("20~30",age_20_30);
        ageCount.put("30~40",age_30_40);
        ageCount.put("40~50",age_40_50“);
        ageCount.put("50~60",age_50_60);
        ageCount.put("60~",age_60_);

        List agePhaseNames=new ArrayList();
        agePhaseNames.add("0~20");
        agePhaseNames.add("20~30");
        agePhaseNames.add("30~40");
        agePhaseNames.add("40~50");
        agePhaseNames.add("50~60");
        agePhaseNames.add("60~");

        *//*agePhase.put("0~20", age_0_20);
        agePhase.put("20~30", age_20_30);
        agePhase.put("30~40", age_30_40);
        agePhase.put("40~50", age_40_50);
        agePhase.put("50~60", age_50_60);
        agePhase.put("60~", age_60_);*//*
        agePhase.add(ageCount);
        agePhase.add(agePhaseNames);
        return agePhase;//"agePhase":[{"name":"0~20","value":age_0_20},{"name":"男","value":6},{"name":"女","value":5}]}*/
    }


    private static int getAgeByBirth(Date birthday) {
        int age = 0;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(new Date());// 当前时间

            Calendar birth = Calendar.getInstance();
            birth.setTime(birthday);

            if (birth.after(now)) {//如果传入的时间，在当前时间的后面，返回0岁
                age = 0;
            } else {
                age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
                if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
                    age += 1;
                }
            }
            return age;
        } catch (Exception e) {//兼容性更强,异常后返回数据
            return 0;
        }
    }
}
